using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CaveArt.Common.Util.IPMonitor
{
    public partial class MainForm : Form
    {
        public enum ExecMode
        {
            CheckIP,
            Config
        }

        private bool bOnTimerProc = false;
        private Timer timer = null;
        private IPMonitor ipMon = null;
        public ExecMode OpMode = ExecMode.CheckIP;
        
        public MainForm()
        {
            InitializeComponent();
            Init();
        }

        public void SetExecMode(string appdatapath, string[] args)
        {
            System.Diagnostics.Debug.Assert(ipMon != null);

            if ( (args.Length == 1 && args[0] == "config") || 
                 !System.IO.File.Exists( appdatapath + "\\" + IPMonitor.CONFIGFILE) )
            {
                IPMonitor.AppDataPath = appdatapath;
                OpMode = ExecMode.Config;
            }
        }

        private void Init()
        {
            ipMon = new IPMonitor();
            this.Visible = false;
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            if (!bOnTimerProc)
            {
                bOnTimerProc = true;
                System.Diagnostics.Debug.Assert(OpMode == ExecMode.CheckIP);

                ipMon.GetConfig();
                if (NetUtil.IsInternetConnected())
                {
                    ipMon.CheckIP();
                }

                DeleteTimer();
                this.Close();
            }
        }

        private void ShowSetupDlg(CaveArt.Common.Util.IPMonitor.Configuration c)
        {
            System.Diagnostics.Debug.Assert(timer == null);
            this.gridConfig.SelectedObject = c;
            this.WindowState = FormWindowState.Normal;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            DeleteTimer();
            this.Close();
        }

        private void DeleteTimer()
        {
            if (this.timer != null)
            {
                this.timer.Stop();
                timer.Tick -= new EventHandler(timer_Tick);
                this.timer.Dispose();
                this.timer = null;
            }
        }

        private void CreateTimer()
        {
            System.Diagnostics.Debug.Assert(timer == null);

            timer = new Timer();
            timer.Interval = 5000;
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            ipMon.CheckIP();
            DeleteTimer();
            this.Close();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            switch (OpMode)
            {
                case ExecMode.Config:
                    ipMon.GetConfig();
                    ShowSetupDlg(ipMon.config);
                    break;
                case ExecMode.CheckIP:
                    this.Visible = false;
                    CreateTimer();
                    break;
            }
        }
    }
}